package leetcode;

import java.util.*;

public class TopStudents {
    public static void main(String[] args) {
        TopStudents topStudents = new TopStudents();
        System.out.println(topStudents.topStudents(
                new String[]{"pyuknvau", "nc", "hwcpe", "wn"},
                new String[]{"bjzzjcgxv", "dibldp", "pmlb"},
                new String[]{"qws z pmlb xmcugvuos lpnygddx bjzzjcgxv hwcpe pmlb pmlb rocg", "hwcpe pyuknvau dibldp bjzzjcgxv bjzzjcgxv hwcpe wn pmlb pyuknvau wodnvd", "dibldp dibldp gna dibldp pmlb pyuknvau pyuknvau nc iygs rkhtnjreq", "pmlb aas wn hwcpe bjzzjcgxv otird pyuknvau fdl bjzzjcgxv dibldp"},
                new int[]{357574114, 899543310, 676388750, 793586311},
                2));
    }

    public List<Integer> topStudents(String[] positive_feedback, String[] negative_feedback, String[] report, int[] student_id, int k) {
        List<Integer> ans = new ArrayList<>();
        Map<Integer, Integer> map = new HashMap<>();
        Map<String, Integer> map1 = new HashMap<>();
        for (String s : positive_feedback) {
            map1.put(s, 3);
        }
        for (String s : negative_feedback) {
            map1.put(s, -1);
        }
        for (int i = 0; i < report.length; i++) {
            String re = report[i];
            int ach = 0;
            String[] reportArray = re.split(" ");
            for (String word : reportArray) {
                ach += map1.getOrDefault(word, 0);
            }
            map.put(student_id[i], ach);
        }
        ArrayList<Map.Entry<Integer, Integer>> list = new ArrayList<>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<Integer, Integer>>() {
            @Override
            public int compare(Map.Entry<Integer, Integer> o1, Map.Entry<Integer, Integer> o2) {
                int res = o2.getValue() - o1.getValue();
                if (res == 0) {
                    return o1.getKey() - o2.getKey();
                }
                return res;
            }
        });
        for (int i = 0; i < k; i++) {
            ans.add(list.get(i).getKey());
        }
        return ans;
    }
}
